layui.define(['table', 'form', 'upload'], function(exports){
  var $ = layui.$,table = layui.table,form = layui.form,upload = layui.upload;

  table.render({
    elem: '#LAY-table'
    ,url: layui.setter.webRoot + '/config/func_model/list.data'
    ,method: 'post'
    ,parseData: function (res) {
        return {
            "code": res.code,
            "msg": res.msg,
            "count": res.body.total,
            "data": res.body.data
        };
    }
    ,response: {statusCode: 200}
    ,size:'lg'
    ,cols: [[
      {field: 'id', title: '功能ID'}
      ,{field: 'name', title: '功能名称', minWidth: 100}
      ,{field: 'typeName', title: '类型'}
      ,{field: 'agentUseCount', title: '代理使用数'}
      ,{field: 'stateName', title: '状态'}
      ,{field: 'iconUrl', title: 'Icon图片', templet: function (record){
              if(record.iconUrl) {
                  return '<img src="'+record.iconUrl+'"  style="max-height:100%;margin:auto;display:block;"/>';
              }else{
                  return '<img src="'+layui.setter.webRoot + '/layuiadmin/style/res/default01.png" style="max-height:100%;margin:auto;display:block;"/>';
              }
          }}
      ,{field: 'url',title: '访问路径'}
      ,{field: 'note',title: '描述'}
      ,{field: 'createDate', title: '创建时间', sort: true}
      ,{title: '操作', width: 120, align:'center', fixed: 'right', toolbar: '#LAY-table-toolbar'}
    ]]
    ,page: true
    ,limit: 30
    ,height: 'full-220'
    ,text: '对不起，加载出现异常！'
  });
  
  //监听搜索
  form.on('submit(LAY-search)', function(data){
    var field = data.field;
    //执行重载
    table.reload('LAY-table', {
      where: field
      ,page: {curr: 1}
    });
  });
  
  //事件
  var active = {
      add:function(){
          layer.open({
              type: 2
              , title: '新增功能模块'
              , content: layui.setter.webRoot + '/config/func_model/add.html'
              , maxmin: false
              , area: ['600px', '500px']
              , btn: ['确定', '取消']
              , yes: function(index, layero){
                  var iframeWindow = window['layui-layer-iframe'+ index]
                      ,submitID = 'LAY-form-submit'
                      ,submit = layero.find('iframe').contents().find('#'+ submitID);
                  iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                      var field = data.field;
                      $.ajax({
                          type: "post",
                          url: layui.setter.webRoot + '/config/func_model/add.do',
                          data:field,
                          dataType:"json",
                          success:function(res){
                              if(res.code == 200){
                                  layer.close(index);
                                  table.reload('LAY-table');
                                  layer.msg('新增成功',{icon: 1});
                              }else{
                                  layer.msg(res.msg,{icon: 2});
                              }
                          }
                      });
                  });
                  submit.trigger('click');
              }
          });
      }
    ,update:function(obj){
          layer.open({
              type: 2
              , title: '修改功能模块'
              , content: layui.setter.webRoot + '/config/func_model/update.html?id='+obj.data.id
              , maxmin: false
              , area: ['600px', '500px']
              , btn: ['确定', '取消']
              , yes: function(index, layero){
                  var iframeWindow = window['layui-layer-iframe'+ index]
                      ,submitID = 'LAY-form-submit'
                      ,submit = layero.find('iframe').contents().find('#'+ submitID);
                  iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                      var field = data.field;
                      $.ajax({
                          type: "post",
                          url: layui.setter.webRoot + '/config/func_model/update.do',
                          data:field,
                          dataType:"json",
                          success:function(res){
                              if(res.code == 200){
                                  layer.close(index);
                                  table.reload('LAY-table');
                                  layer.msg('修改成功',{icon: 1});
                              }else{
                                  layer.msg(res.msg,{icon: 2});
                              }
                          }
                      });
                  });
                  submit.trigger('click');
              }
          });
      }
    ,del:function(obj){
    	layer.confirm('确定要删除该功能模块吗？', function(index){
	    	$.ajax({
	      	  type: "post",
	          url: layui.setter.webRoot + '/config/func_model/del.do',
	          data:{id:obj.data.id},
	  		  dataType:"json",
	  		  success:function(res){
	  			if(res.code == 200){
	  				obj.del();
	  			}else{
	  				layer.msg(res.msg,{icon: 5});
	  			}
	  		  }
	        });
	    	layer.close(index);
	    });
     }
	 ,exportToExcel: function(){ //导出
    	layer.confirm('确定要导出全部数据吗？', function(index){
   			$('#exportExcelForm').submit();
        	layer.close(index);
        });
     }
  };
  
  //导入
  upload.render({
    elem: '#uploadExcel' //绑定元素
    ,url: '$!{webRoot}/config/func_model/uploadExcel.do' //上传接口
    ,before: function(obj){
       layer.load(); //上传loading
    }
    ,done: function(res){
  		if(res.code == 200){
  			 table.reload('LAY-list'); //数据刷新	
  			 layer.msg(res.msg,{icon:1});
  		}else{
  			 layer.msg(res.msg,{icon: 5});
  		}
      //上传完毕回调
     
      layer.closeAll('loading'); //关闭loading
     
    },error: function(res){
      //请求异常回调
      table.reload('LAY-list'); //数据刷新
      layer.closeAll('loading'); //关闭loading
      layer.msg(res.msg,{icon: 5});
    }
    ,accept: 'file'
  });
  
  $('.layui-btn.LAY-btn-fun').on('click', function(){
    var type = $(this).data('type');
    active[type] ? active[type].call(this) : '';
  });
  
  table.on('tool(LAY-table)', function(obj){
  	var type = obj.event ;
   	active[type] ? active[type].call(this,obj) : '';
  });
  
  exports('func_model_list', {})
});